(function() {

this.pasta.webgui._mapLoaded = false;
this.pasta.webgui._mapWaitList = new pasta.util.ArrayList();

this._mapInitialize = function()
{
	pasta.webgui._mapLoaded = true;
	
	for (var i = 0; i < pasta.webgui._mapWaitList.getLength(); i++)
	{
		pasta.webgui._mapWaitList.get(i).initMap();
	}
}

this.pasta = this.pasta || {};
this.pasta.webgui = this.pasta.webgui || {};

this.pasta.webgui.Map = pasta.webgui.Control.extend(
{
	init : function() 
	{
		this._super();
		this.zoom = 6;
		this.inited = false;
		
		if (!pasta.webgui._mapLoaded)
		{
			var script = document.createElement("script");
			script.type = "text/javascript";
			script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=_mapInitialize";
			document.body.appendChild(script);
		}		
		
		this._onReady = null;
		
		this.initMap();
	},
	
	initMap : function()
	{
		if (pasta.webgui._mapLoaded && !this.inited)
		{
			if (document.getElementById(this.cid) != null)
			{
				var myLatlng = new google.maps.LatLng(-6.189366, 106.799469);
				var myOptions = {
					zoom: this.zoom,
					center: myLatlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					mapTypeControl: true,
					mapTypeControlOptions: {
						style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
						position: google.maps.ControlPosition.TOP_RIGHT
					},
				}
				
				this.map = new google.maps.Map(document.getElementById(this.cid), myOptions);
			
				this.overlay = new google.maps.OverlayView();
				this.overlay.draw = function() {};
				this.overlay.setMap(this.map);
				
				this.inited = true;
				
				if (this._onReady != null)
					this._onReady.callHandler(this);
			}
			else
			{
				var target = this;
				setTimeout(function() {target.initMap()}, 100);
			}
		}
		else
		{
			if (pasta.webgui._mapWaitList.indexOf(this) < 0)
				pasta.webgui._mapWaitList.add(this);
		}
	},
	
	latLngToPixel : function(location)
	{
		return this.overlay.getProjection().fromLatLngToContainerPixel(location);
	},

	setMapType : function(type)
	{
		switch (type)
		{
			case 1 : // terrain
					this.map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
					break;
			case 2 : // satellite
					this.map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
					break;
			case 3 : // road
					this.map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
					break;
		}
	},
	
	setZoom : function(zoom)
	{
		this.zoom = zoom;
		
		if (this.map != null)
			this.map.setZoom(zoom);
	},
	
	setCenter : function(lat, lng)
	{
		if (this.map != null)
			this.map.setCenter(new google.maps.LatLng(lat, lng));
	},
	
	doAddedToStage : function()
	{
		this._super();
		
		if (!this.inited)
			this.initMap();
	},
	
	//----------------------- Event -----------------------
	
	getOnReady : function()
	{
		if (this._onReady == null)
			this._onReady = new pasta.util.EventObj();
		
		return this._onReady;
	}
	
	//----------------------- Setter & Getter ------------------
});

})();